//
//  MyHomeworkViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/7.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "MyHomeworkViewController.h"
#import "SendFriendCircleViewController.h"

@interface MyHomeworkViewController ()

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MyHomeworkViewController
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"作业列表";
    CGRect rect = self.view.bounds;
    rect.size.height -= 44 + 64;
    if (self.homeworkType == KBackHomeWorkType) {
        rect.size.height -= 70;
        [self creatFootView];
    }
    self.tableView.frame = rect;
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"上传" target:self selector:@selector(uploadHomeWork)];
    [self setupRefresh];

}
- (void)creatFootView{
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, kScreenH - 44-64-70, kScreenW, 70);
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 10, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"上传作业"];
    [completeButton addTarget:self action:@selector(uploadHomeWork) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:completeButton];
    
}
#pragma mark - 上传作业
- (void)uploadHomeWork{
    SendFriendCircleViewController *sendFriendCircleVC = [[ SendFriendCircleViewController alloc]init];
    sendFriendCircleVC.sendType = KSendHomeWork;
    sendFriendCircleVC.titleStr = @"提交作业";
    sendFriendCircleVC.courseId = @"0";
    sendFriendCircleVC.placeholderStr = @"请上传你的作业......";
    __weak typeof (self)unself = self;
    sendFriendCircleVC.sendFriendCircleBlock = ^(){
        [unself setupRefresh];
    };
    [self.navigationController pushViewController:sendFriendCircleVC animated:YES];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%ld",(long)self.pageNo]  forKey:@"page"];   //页数 默认1
    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    
    NSString *url;
    if (self.homeworkType == KCurrentHomeWorkType) {//课堂作业
        url = [JSPathUtil urlAppCourseGetHomeworkList];
    }else if (self.homeworkType == KBackHomeWorkType) {//课后作业
        [dic setValue:@"4" forKey:@"topic_type"];
        url = [JSPathUtil urlAppFriendGetDiaryList];
    }
    
    [yjNetWorkManger requestType:GET andURL:url andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            if (self.pageNo == 1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:response[@"list"]];
            [self refreshCount:self.dataArray.count noDataText:@"还没有作业" totalRow:response[@"totalRow"]];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
   
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *label = [[UILabel  alloc]init];
        label.tag = 1000;
        label.font = [UIFont systemFontOfSize:14];
        label.numberOfLines = 0;
        [cell.contentView addSubview:label];
        
        UIImageView *commentImageView=[[UIImageView alloc]initWithFrame:CGRectMake( kScreenW - 20 - 25, 12, 20, 20)];
        commentImageView.tag = 1001;
        commentImageView.contentMode=UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:commentImageView];
    }
    NSDictionary *homeworkDic = [[NSDictionary alloc]initWithDictionary:self.dataArray[indexPath.row]];
    NSString *titleStr = strIsEmpty(homeworkDic[@"context"]) ?@"":homeworkDic[@"context"] ;//context
    if (self.homeworkType == KBackHomeWorkType) {
        titleStr = strIsEmpty(homeworkDic[@"topic_context"]) ?@"":homeworkDic[@"topic_context"] ;//context
    }
    NSArray *imageArray = [NSArray arrayWithArray:homeworkDic[@"image_list"]];//image_list
   
    UILabel *label = [cell.contentView viewWithTag:1000];
    label.text = titleStr;
    label.frame = CGRectMake(10, 10, kScreenW - 20, [Global getSizeOfString:titleStr maxWidth:kScreenW - 20 maxHeight:CGFLOAT_MAX withFontSize:14.6].height + 10) ;
    
    UIImageView *commentImageView = [cell.contentView viewWithTag:1001];
    if (!imageArray.count || strIsEmpty(imageArray[0])) {
        commentImageView.frame=CGRectZero;
        [self removeallSubViews:commentImageView];
    }
    else
    {
        [self removeallSubViews:commentImageView];
        commentImageView.frame= CGRectMake(10, CGRectGetMaxY(label.frame), kScreenW - 20, imageArray.count? (imageArray.count / 3)*( kScreenW-60)/3.0 : 0);
        for (int i=0; i < imageArray.count; i++) {
            UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake((( kScreenW-60)/3.0 + 15) * (i%3), (( kScreenW-60)/3.0 + 15) * (i / 3), ( kScreenW-60)/3.0, ( kScreenW-60)/3.0)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] placeholderImage:KSIDEDEFAULTHEAD];
            imageView.tag=100+i;
            imageView.backgroundColor= [UIColor clearColor];
            imageView.contentMode=UIViewContentModeScaleToFill;
            [commentImageView addSubview:imageView];
        }
    }
    return cell;
}
/**
 *  移除视图
 *
 *  @param supView 父视图
 */
- (void )removeallSubViews:(UIView *)supView{
    for (id obj in supView.subviews) {
        [obj removeFromSuperview];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *homeworkDic = [[NSDictionary alloc]initWithDictionary:self.dataArray[indexPath.row]];
    NSString *titleStr = strIsEmpty(homeworkDic[@"context"]) ?@"":homeworkDic[@"context"] ;//context
    NSArray *imageArray = [NSArray arrayWithArray:homeworkDic[@"image_list"]];//image_list
    if (self.homeworkType == KBackHomeWorkType) {
        titleStr = strIsEmpty(homeworkDic[@"topic_context"]) ?@"":homeworkDic[@"topic_context"] ;//context
    }
    
    double titleHeight = [Global getSizeOfString:titleStr maxWidth:kScreenW - 20 maxHeight:CGFLOAT_MAX withFontSize:14.6].height + 20;
    double imageHeight = imageArray.count? ((imageArray.count -1) / 3 + 1)*(( kScreenW-60)/3.0 + 10 ): 0;
    
    return titleHeight + imageHeight + 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    ResultInputViewController *resultInputVC = [[ResultInputViewController alloc]init];
//    [self.navigationController pushViewController:resultInputVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
